package com.haoshui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoshui.constants.SystemConstants;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.entity.Article;
import com.haoshui.domain.entity.Category;
import com.haoshui.domain.vo.CategoryListVo;
import com.haoshui.mapper.CategoryMapper;
import com.haoshui.service.ArticleService;
import com.haoshui.service.CategoryService;
import com.haoshui.utils.BeanCopyUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-09-26 16:01:38
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取可见的category列表,前台只查询有文章的种类
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {

        //查询文章表，获得categoryID
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articles = articleService.list(queryWrapper);

        //筛选categoryId，可见,去重
        Set<Long> categoryIds = articles.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());

        //查询category表, 查找可见的category，  获得信息
        List<Category> categories = listByIds(categoryIds);

        categories =  categories.stream().filter(category -> SystemConstants.CATEGORY_STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //信息规范化,转成Vo
        List<CategoryListVo> categoryListVos = BeanCopyUitls.copyBeanList(categories, CategoryListVo.class);
        return ResponseResult.okResult(categoryListVos) ;
    }


    /**
     * 导出种类到excel
     * @return
     */
    @Override
    public ResponseResult export() {
        return ResponseResult.okResult();
    }

    /**
     * 获取所有可用的category，
     * @return List<>Cateory</>
     */
    @Override
    public List<Category> allCategoryList() {
        //查询状态为0的category
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus, SystemConstants.CATEGORY_STATUS_NORMAL);
        List<Category> list = list(queryWrapper);

        //返回数据
        return list;

    }


    /**
     * 分页查询category，有条件就模糊查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public Page<Category> pageCategory(Integer pageNum, Integer pageSize, String name, String status) {
        //设置模糊查询的条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(status) && StringUtils.hasText(status), Category::getStatus, status);
        queryWrapper.like(Objects.nonNull(name) && StringUtils.hasText(name), Category::getName, name);

        //设置页
        Page<Category> page = new Page<>(pageNum, pageSize);

        //查询
        page(page, queryWrapper);

        return page;
    }
}
